package com.mytest.provider.qz.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mytest.provider.qz.SampleJob;
import com.mytest.provider.qz.controller.HelloJob;
import org.quartz.*;
import org.quartz.spi.JobFactory;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;

/**
 * @Author: llq
 * @Date: 2020/5/9
 */
@Configuration
public class QuartzConfig {

   /* //执行任务。有了触发器，我们就可以执行任务了。注册一个SchedulerFactroyBean,然后将触发器一list的方式传入
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(DruidDataSource druidDataSource, @Qualifier("jobOneTrigger") Trigger jobOneTrigger, @Qualifier("jobTwoTrigger") Trigger jobTwoTrigger){
        SchedulerFactoryBean schedulerFactoryBean=new SchedulerFactoryBean();
        //调度器名称
        schedulerFactoryBean.setSchedulerName("TestScheduler");
        //数据源
        schedulerFactoryBean.setDataSource(druidDataSource);
        //覆盖已存在的任务,用于Quartz集群，QuartzScheduler启动会更新已存在的Job
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        //延时1s启动定时任务，避免系统未完全启动却开始执行定时任务的情况
        schedulerFactoryBean.setStartupDelay(1);
        //设置加载的quartz.properties配置文件
        schedulerFactoryBean.setConfigLocation(new ClassPathResource("/quartz.properties"));
        //自动启动
        schedulerFactoryBean.setAutoStartup(true);
        //注册触发器
        schedulerFactoryBean.setTriggers(jobOneTrigger,jobTwoTrigger);
        return schedulerFactoryBean;
    }*/






  /*  @Bean
    public JobDetail uploadTaskDetail() {
        return JobBuilder.newJob(SampleJob.class)   //SampleJob我们的业务类
//                .withIdentity("start_of_day", "start_of_day")
                .withIdentity("mjtTask")             //给Trigger起个名字
                .storeDurably()                     //即使没有Trigger关联时，也不需要删除该JobDetail
                .build();
    }*/

 /*   @Bean
    public Trigger uploadTaskTrigger() {

        //固定间隔  5秒执行一次
*//*        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(50)
                .repeatForever();*//*
        // cron 表达式  5秒执行一次
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
//        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 24 18 * * ?");


        return TriggerBuilder.newTrigger()
                .forJob(uploadTaskDetail())          //关联上述的JobDetail
//                .withIdentity("start_of_day", "start_of_day")
                .withIdentity("mjtTask")             //给Trigger起个名字
//                .startNow()
                .withSchedule(scheduleBuilder)
                .build();
    }*/

//---------------------------------------------------------------------------------
    @Autowired
    private JobFactory jobFactory;

//    private JobFactory jobFactory;

    @Autowired
    private AutowireCapableBeanFactory  capableBeanFactory;


     /**
      * 当触发器触发时，与之关联的任务被Scheduler中配置的JobFactory实例化，也就是每触发一次，就会创建一个任务的实例化对象
      * (如果缺省)则调用Job类的newInstance方法生成一个实例
      * (这里选择自定义)并将创建的Job实例化交给IoC管理
      * @return
      * */
    @Bean
    public JobFactory jobFactory() {
        return new AdaptableJobFactory() {
            @Override
            protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
                Object jobInstance = super.createJobInstance(bundle);
                capableBeanFactory.autowireBean(jobInstance);
                return jobInstance;
            }
        };
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setJobFactory(jobFactory);
        //延迟启动
        schedulerFactoryBean.setStartupDelay(1);
        return schedulerFactoryBean;
    }

    @Bean(name = "scheduler")
    public Scheduler scheduler() {
        return schedulerFactoryBean().getScheduler();
    }

//    --------------------------------------------------------------------------------

/*    @Bean //job配置
    JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(HelloJob.class);
        return jobDetailFactoryBean;
    }


    @Bean  //调度器配置
    CronTriggerFactoryBean cronTriggerFactoryBean(){
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean().getObject());
        cronTriggerFactoryBean.setCronExpression("0/2 * * * * ?");
        return cronTriggerFactoryBean;
    }

    @Bean  //中心配置
    SchedulerFactoryBean schedulerFactoryBean(){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setTriggers(cronTriggerFactoryBean().getObject());
        return schedulerFactoryBean;
    }

    @Bean(name="Scheduler")  //获得可以操作定时任务的对象
    public Scheduler scheduler() {
        return schedulerFactoryBean().getScheduler();
    }*/

//    ---------------------------------------------------------------------

 /*   @Bean  //中心配置
    public SchedulerFactoryBean schedulerFactoryBean(){
        //        schedulerFactoryBean.setDataSource(datasource());

        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        return schedulerFactoryBean;
    }


    //获得可以操作定时任务的对象
    @Bean(name="Scheduler")
    public Scheduler scheduler() {
        return schedulerFactoryBean().getScheduler();
    }*/

   /* @Bean //数据库连接池
    public DruidDataSource datasource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/sc_mt");
        druidDataSource.setUsername("ENC(LShBNSq6WP1jCb7YHvDAEQ==)");
        druidDataSource.setPassword("ENC(I8PkwICJHBNXCMTjJNHfwjPs65tFpMPv)");
        druidDataSource.setMaxActive(20);
        druidDataSource.setMaxWait(6000);
        druidDataSource.setInitialSize(1);
        return druidDataSource;
    }*/


}





